package com.mxf.code.config;

import com.alibaba.druid.pool.DruidDataSource;
import com.mxf.code.properties.DataSourceProperties;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.boot.CommandLineRunner;

import java.util.HashMap;
import java.util.Map;

/**
 * 初始化租户数据源
 *
 * @author mxf
 */
public class InitDataSource implements CommandLineRunner {
    @Autowired
    private DynamicDataSource dynamicDataSource;

    @Override
    public void run(String... args) {
        Map<Object, Object> dataSources = new HashMap<>();
        DataSourceProperties dataSourceProperties = new DataSourceProperties();
        dataSourceProperties.setDriverClassName("com.mysql.cj.jdbc.Driver");
        dataSourceProperties.setUrl("jdbc:mysql://localhost:3306/dynamic01?serverTimezone=Asia/Shanghai&allowMultiQueries=true");
        dataSourceProperties.setUsername("root");
        dataSourceProperties.setPassword("root");
        DruidDataSource druidDataSource = DynamicDataSourceFactory.buildDruidDataSource(dataSourceProperties);
        dataSources.put("1", druidDataSource);
        dynamicDataSource.addDataSources(dataSources);
    }
}